const { ccclass, property } = cc._decorator;

import AudioManager from './AudioManager';
import AbTestMgr from './AbTestMgr';
import PeripheralSystemDataMgr from './PeripheralSystemDataMgr';
import EventMgr from './EventMgr';
import MatchGameEventType from './MatchGameEventType';
import AdManager from './AdManager';
import SdkHelper from './SdkHelper';
import MatchGameData from './MatchGameData';
import MatchGameAudioMgr from './MatchGameAudioMgr';
import BasePageCtrl, { AnimType } from './BasePageCtrl';
import MatchGameSystemMgr from './MatchGameSystemMgr';
import UiManage from './UiManage';
import MatchGameFailPage from './MatchGameFailPage';
import NativeUtils from './NativeUtils';

@ccclass
export default class MatchGameFailPageCtrl extends BasePageCtrl {

    ui: MatchGameFailPage = null;

    onLoad() {
        this.onUILoad();
        this._animType = AnimType.SCALE;
        this._touchControl = false;
        this._hasPeneLock = true;
        this._hasBlack = true;
        this._hasTouchLock = false;
        this._hasBlackTouch = false;
        this.addButtonListen();
        super.onLoad();
    }

    start() { }

    onUILoad() {
        this.ui = this.node.addComponent(MatchGameFailPage);
    }

    _init() {
        var abStepFlag = AbTestMgr.ab_add_step_flag;
        if (abStepFlag === "s0") {
            let spriteFramePath = "imgs/game/matchMain/matchGameFailPage/addStep_1";
            UiManage.loadSpriteFrameInDeepPath(this.ui.addStepSpr, spriteFramePath);
            this.ui.tipLable.getComponent(cc.Label).string = "增加10步继续玩！";
        } else if (abStepFlag === "s1") {
            let spriteFramePath = "imgs/game/matchMain/matchGameFailPage/addStep_2";
            UiManage.loadSpriteFrameInDeepPath(this.ui.addStepSpr, spriteFramePath);
            this.ui.tipLable.getComponent(cc.Label).string = "增加15步继续玩！";
        } else if (abStepFlag === "s2") {
            let spriteFramePath = "imgs/game/matchMain/matchGameFailPage/addStep_3";
            UiManage.loadSpriteFrameInDeepPath(this.ui.addStepSpr, spriteFramePath);
            this.ui.tipLable.getComponent(cc.Label).string = "增加20步继续玩！";
        }
        EventMgr.trigger(MatchGameEventType.HIDE_MATCHGAME_NODE);
        MatchGameAudioMgr.playFailShowAudio();
        this.ui.btn_close.active = false;
        this.node.setContentSize(cc.winSize);
        PeripheralSystemDataMgr.setRollingNoticeParent(this.ui.noticeParent);
        AudioManager.getInstance().pauseMusic("bgm", true);
    }

    addButtonListen() {
        UiManage.addButtonListen(this.ui.btn_close, this.clickClose, this);
        UiManage.addButtonListen(this.ui.btn_continue, this.continueCallback, this);
        UiManage.addButtonListen(this.ui.btn_giveUp, this.giveupCallback, this);
    }

    clickClose() {
        EventMgr.trigger(MatchGameEventType.SHOW_MATCHGAME_NODE);
        this.hide();
    }

    public _onShow(): void {
        super._onShow();
        NativeUtils.showScreenAds(6, () => { }, true)
    }

    public hide(): void {
        super.hide();
        NativeUtils.showScreenAds(6, () => { }, false)
    }

    continueCallback() {
        PeripheralSystemDataMgr.reportEncourageVideo();
        NativeUtils.showVideoAd(6, () => {
            var totalSteps = MatchGameData.total_step + MatchGameData.video_step;
            MatchGameSystemMgr.addStepVideo(totalSteps, () => {
                MatchGameAudioMgr.playFailAddStepAudio();
                SdkHelper.reportData("matchgame_click_continue", { level: MatchGameData.gameLevel, game_time: MatchGameData.getGameTime() });
                AudioManager.getInstance().resumeMusic("bgm", true);
                EventMgr.trigger(MatchGameEventType.SHOW_MATCHGAME_NODE);
                this.hide();
            });
        });
        NativeUtils.report2('level_fail', { level_id: MatchGameData.gameLevel, revive: 1 });
    }

    giveupCallback() {
        MatchGameAudioMgr.playFailExitAudio();
        this.hide();
        NativeUtils.report2('level_fail', { level_id: MatchGameData.gameLevel, revive: 0 });
        EventMgr.trigger(MatchGameEventType.END_MATCH_GAME, { is_tg: 0 });
    }

    _onHide() {
        super._onHide();
    }

    static prefabUrl = 'MatchGameFailPage';
    static className = 'MatchGameFailPageCtrl';
}





